var client = new OSS.Wrapper({//新建一个对象存储对象
    region : 'oss-cn-beijing',//对象存储（数据中心）所在地
    accessKeyId : 'LTAIJWjMhY4pWJw4',//申请的akID
    accessKeySecret : 'XUPmliAxUREWNu9bv1ORUv9Iq0scCB',//申请的ak密码
    bucket : 'senmiao1'//oss对象存储文件夹的名字
});

var upload_input=$('label input');
upload_input.on('change',function(){
    var f = $(this)[0].files[0];//file文件里面的下相关属性，如name，lastModified最后修改等
    var val= $(this).val();
    var img=$(this).prev();

    var suffix = val.substr(val.indexOf("."));
    var obj=timestamp();  // 这里是生成文件名
    var storeAs = 'upload-file/'+obj+suffix;  //命名空间

    var resume = {};
    client.multipartUpload(storeAs, f).then(function (result) {//把图片地址上传到oss再返回调用结果（url）
        img.attr('src',result.url);
        //var str_href=window.location.href.split('?')[0];
        //
        //if( str_href == server + '/edit-resume.html') {
        //    $('#portrait').attr('src',result.url);
        //
        //    var id = '';
        //    var url1 = location.search;
        //    if (url1.indexOf("?") != -1) {
        //        id = url1.substr(4);
        //    }
        //    resume = {"id": id, "photo": result.url};
        //    var url2 = server + '/server/resume/setUpResume.json';
        //    $.post(url2, resume, function (data) {
        //        if (data.code == 200) {
        //            alert('保存成功');
        //        }
        //    });
        //}
        //else {
        //    $('#portrait2').attr('src',result.url);
        //    alert('上传成功');
        //}
    }).catch(function (err) {
        console.log(err);
    });
});

function timestamp(){
    var time = new Date();
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return ""+y+add0(m)+add0(d)+add0(h)+add0(mm)+add0(s);
}

function add0(m){//判断‘月’前面是否加0
    return m<10?'0'+m : m;
}


$('#next-step').on('click',function(){
    var userName = $('.userName input').val();
    var idCode = $('.idCode input').val();
    var frontPhoto = $('.front.photo').attr('src');
    var backPhoto = $('.back.photo').attr('src');

    if(userName!=''&& idCode != '') {

        if (frontPhoto !== '' && backPhoto !== '') {

            var url = server +'/server/authentication/identity.json';
            var args = {"card_number":idCode,"name":userName,"positivePhoto":frontPhoto,"oppositePhoto":backPhoto};
            $.post(url, args, function (data) {
                if(data.code == 20000){
                    alert('认证成功');
                    window.location.assign('replace-card-success.html');
                }
            });
        }
        else {
            alert('图片上传不能为空');
        }

    }

    else{
        alert('用户名或身份证格式不正确');
    }
});